/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_KOBJECT_H__
#define __EB_KOBJECT_H__


#include <types.h>
#include <list.h>


/* Collection of operations for attribute files */
struct kobj_ops;


/**
 * @brief: The root object is the system's root directory, and all objects should be located below it
 * 
 * @name: directory or attribute files name
 * @kops: pointer kobj operation api
 * @parent: parent object
 * @entry: link list same level objects
 * @child: link list child all objects
 * @kref: reference count
 * @release: Release function pointer
 * @priv: Private data pointer
 */
typedef struct kobj {
    /* directory or attribute files name */
    const char        *name;
    /* pointer kobj operation api */
    struct kobj_ops   *kops;
    /* pointer kobj parent */
    struct kobj       *parent;
    /* pointer brother kobj*/
    struct list_node  entry;
    /* pointer sub directory */
    struct list_node  child;
    /* reference count */
    size_t            kref;
    /* kobj release memory */
    void (*release)(struct kobj *kobj);
    /* pointer private data */
    void              *priv;
}kobj_t;


typedef struct kobj_ops {
    /* kobj read */
    size_t (*read)(struct kobj *kobj, void *buf, size_t size);
    /* kobj write */
    size_t (*write)(struct kobj *kobj, void *buf, size_t size);
}kobj_ops_t;


typedef size_t (*kobj_read_t)(struct kobj *kobj, void *buf, size_t size);
typedef size_t (*kobj_write_t)(struct kobj *kobj, void *buf, size_t size);


#ifdef __cplusplus
extern "C" {
#endif


kobj_t* kobj_get_root(void);
bool kobj_is_root(kobj_t *kobj);
bool kobj_is_dir(kobj_t *kobj);
bool kobj_is_attr(kobj_t *kobj);
bool kobj_is_child(kobj_t *parent, kobj_t *kobj);
void kobj_root_init(void);
void kobj_init(kobj_t *kobj, const char *name, struct kobj_ops *kops);
void kobj_set_parent(kobj_t *kobj, kobj_t* parent);
kobj_t* kobj_alloc(const char *name, struct kobj_ops *kops);
kobj_t* kobj_alloc_dir(const char *name);
kobj_t* kobj_alloc_attr(const char *name, struct kobj_ops *ops);
bool kobj_add(kobj_t *parent, kobj_t *kobj);
bool kobj_remove(kobj_t *parent, kobj_t *kobj);
kobj_t* kobj_find_by_name(kobj_t *parent, const char *name);
kobj_t* kobj_create_dir(kobj_t *parent, const char *name);
kobj_t* kobj_create_attr(kobj_t *parent, const char *name, struct kobj_ops *kops);
bool kobj_remove_self(kobj_t *kobj);


#ifdef __cplusplus
}
#endif

#endif //!__EB_KOBJECT_H__
